package com.canbot.u05.activity.training;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * 培训主持模式选择音频界面
 */
public class TrainingChooseAudioActivity extends BaseActivityNoTitle implements View.OnClickListener, AdapterView.OnItemClickListener {

        private List<Audio> data = new ArrayList<>();

        private Audio selectAudio;  //选择的音频

        private Dialog dialogLoading;

        private GridView gridView;

        private int selectedIndex = -1;

        private String oldPath =null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_choose_audio);
                oldPath = getIntent().getStringExtra("mPicPath");
                setViews();
                getData();
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        private void setViews() {

                findViewById(R.id.bt_back).setOnClickListener(this);
                findViewById(R.id.bt_close).setOnClickListener(this);

                gridView = (GridView) findViewById(R.id.training_mode_choose_pic_gv);
                gridView.setAdapter(myBaseAdapter);
                myBaseAdapter.notifyDataSetChanged();
                gridView.setOnItemClickListener(this);

        }

        private void getData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(TrainingChooseAudioActivity.this, getResources().getString(R.string.request));
                dialogLoading.show();
                String url = "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/getAudioList";
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("path", "/uusound");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("t", result);
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.setCancelable(true);
                                                        dialogLoading.dismiss();
                                                        List<Audio> list = JSON.parseArray(strMsgBean.getResult().toString(), Audio.class);

                                                        Collections.sort(list, new Comparator<Audio>() {
                                                                @Override
                                                                public int compare(Audio audio, Audio t1) {
                                                                        return audio.getName().compareTo(t1.getName());
                                                                }
                                                        });

                                                        data.addAll(list);
                                                        myBaseAdapter.notifyDataSetChanged();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(TrainingChooseAudioActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.bt_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.bt_close:
                                if (selectAudio == null) {
                                        ToastTools.newInstance(TrainingChooseAudioActivity.this).showText(getString(R.string.please_select_audio));
                                        return;
                                }
                                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND,"stop_sound");
                                Intent intent = new Intent();
                                intent.putExtra("mPicPath", selectAudio.getPath());
                                setResult(RESULT_OK, intent);
                                finish();
                                break;

                }
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                selectedIndex = position;
                selectAudio = data.get(position);
                boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, selectAudio.getPath());
                Logger.e("t", "播放路径==" + selectAudio.getPath());
                if (!isSuccess) {
                        ToastTools.newInstance(TrainingChooseAudioActivity.this).showText(getString(R.string.network_error));
                }
                myBaseAdapter.notifyDataSetChanged();
        }


        BaseAdapter myBaseAdapter = new BaseAdapter() {


                @Override
                public int getCount() {
                        return data == null ? 0 : data.size();
                }

                @Override
                public Object getItem(int position) {
                        return position;
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;
                        if (convertView == null) {
                                convertView = View.inflate(TrainingChooseAudioActivity.this, R.layout.gridview_training_host_choose, null);
                                holder = new ViewHolder();
                                holder.audioName = (TextView) convertView.findViewById(R.id.aduio_name);
                                holder.trainingCheck = (ImageView) convertView.findViewById(R.id.iv_check);
                                holder.trainingCheckbg = (ImageView) convertView.findViewById(R.id.iv_check_bg);

                                convertView.setTag(holder);
                        }
                        else {
                                holder = (ViewHolder) convertView.getTag();
                        }

                        holder.audioName.setText(data.get(position).getName());

                        if (data.get(position).getPath().equals(oldPath)) {
                                holder.trainingCheck.setVisibility(View.VISIBLE);
                                holder.trainingCheckbg.setVisibility(View.VISIBLE);
                                oldPath = null;
                        }else if (position == selectedIndex) {
                                holder.trainingCheck.setVisibility(View.VISIBLE);
                                holder.trainingCheckbg.setVisibility(View.VISIBLE);
                        }
                        else {
                                holder.trainingCheck.setVisibility(View.INVISIBLE);
                                holder.trainingCheckbg.setVisibility(View.INVISIBLE);
                        }

                        return convertView;
                }

                class ViewHolder {

                        TextView audioName;

                        ImageView trainingCheck;

                        ImageView trainingCheckbg;

                }

        };

        @Override
        protected void onPause() {
                super.onPause();
        }
}
